package com.macaline.service.impl.user;

import com.macaline.mapper.BaseMapper;
import com.macaline.mapper.UserMapper;
import com.macaline.entity.User;
import com.macaline.service.base.BaseService;
import com.macaline.service.user.UserService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceimpl extends BaseService<User>  implements UserService{
	
	@Resource
	UserMapper userMapper;
	

	@Override
	@Cacheable(value = "user" , key = "#id")
	public User getUser(Integer id) {
		return userMapper.selectById(id);
	}

	@Override
	public void save(User user) {
		if(user.getId() == null) {
			userMapper.insert(user);
		}else {
			userMapper.insert(user);
		}
	}

	@Override
	public User getByUserName(String userName) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_name",userName);
		List<User> users = userMapper.selectByMap(map);
		return users.get(0);
	}

	@Override
	public BaseMapper<User> getMapper() {
		return userMapper;
	}
}
